package com.young.mylib.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 戈非 on 2016/4/8.
 */
public class Utils {
    private static final String TAG = "Utils";

    public static String asciiToString(String value)
    {
        String s="";
        try {
            StringBuffer sbu = new StringBuffer();
            String[] chars = value.split(",");
            for (int i = 0; i < chars.length; i++) {
                sbu.append((char) Integer.parseInt(chars[i]));
            }
            s=sbu.toString();
        }catch (Exception e){
            return  s;
        }
        return s ;
    }
    public static int str2ascii(String s) {// 字符串转换为ASCII码
        int i = 0;
        try {
            char[] chars = s.toCharArray();
            i = (int) chars[0];
        } catch (Exception e) {
            return i;
        }
        return i;
    }

    public static int str2int(String str) {
        int r = 0;
        try {
            r = Integer.parseInt(str);
        } catch (Exception e) {
            return r;
        }
        return r;
    }

    public static String int2str(int i) {
        String r = "";
        try {
            r = String.valueOf(i);
        } catch (Exception e) {
            return r;
        }
        return r;
    }

    public static String formatDateStr(final long time) {

        try {

            Date d = new Date(time);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(d);
        } catch (Exception e) {

            //e.printStackTrace();
        }
        return null;
    }
    private byte[] getHexBytes(String message) {
        int len = message.length() / 2;
        char[] chars = message.toCharArray();
        String[] hexStr = new String[len];
        byte[] bytes = new byte[len];
        for (int i = 0, j = 0; j < len; i += 2, j++) {
            hexStr[j] = "" + chars[i] + chars[i + 1];
            bytes[j] = (byte) Integer.parseInt(hexStr[j], 16);
        }
        return bytes;
    }
    public static String getPkgName(Context context) {
        try {
            String pkName = context.getPackageName();

            return pkName;
        } catch (Exception e) {
        }
        return "";
    }
    public static String getSDPath() {
        File sdDir = null;

        boolean sdCardExist = Environment.getExternalStorageState()
                .equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();//获取跟目录
            // Log.i(TAG, sdDir.toString() + ":" + sdDir.getPath());
            return sdDir.toString();
        } else {

        }
        return null;
    }


    /**
     * 判断服务是否运行
     *
     * @param mContext
     * @param serviceName
     * @return
     */
    public static boolean isServiceWork(Context mContext, String serviceName) {
        boolean isWork = false;
        ActivityManager myAM = (ActivityManager) mContext
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> myList = myAM.getRunningServices(40);
        if (myList.size() <= 0) {
            return false;
        }
        for (int i = 0; i < myList.size(); i++) {
            String mName = myList.get(i).service.getClassName().toString();
            if (mName.equals(serviceName)) {
                isWork = true;
                break;
            }
        }
        Log.e(TAG, "Service is work: " + isWork);
        return isWork;
    }
}
